/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package eSales.com.BL;

import eSales.com.DAL.Lot;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author karamelka
 */
public class UserContentServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        EntityManagerFactory emf = Persistence.createEntityManagerFactory("eSalesPU");
        EntityManager em = emf.createEntityManager();

        try {
            /* TODO output your page here. You may use following sample code. */
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Show Lots servlet</title>");
            out.println("<link type=\"text/css\" rel=\"stylesheet\" href=\"./resources/css/default.css\">");
            out.println("<link type=\"text/css\" rel=\"stylesheet\" href=\"./resources/css/cssLayout.css\">");
            out.println("</head>");
            out.println("<body>");
   //         out.println("<h1>Servlet UserContentServlet at " + request.getContextPath() + "</h1>");

            List<Lot> tst = em.createNamedQuery("Lot.findAll").getResultList();
            out.println("<table class=\"tableTr\">");
//            int count = -1;
            out.println("<tr>");
            out.println("<th width='150'>Name</th>");
            out.println("<th width='100'>Start Price</th>");
            out.println("<th width='150'>End Time</th>");
            out.println("<th width='100'>More info</th>");
            out.println("</tr>");
            for (Lot l : tst) {
//                count++;
//                if (count % 2 == 0) {
//                    out.println("<tr>");
//                }

                try {
                    out.println("<tr>");
                    out.println("<td>" + l.getName() + "</td>");
                    out.println("<td>" + l.getStartPrice() + "</td>");
                    if (l.getType().equals("tender")) {
                        out.println("<td>" + new SimpleDateFormat("dd/MM/yyyy HH:mm:ss", Locale.ENGLISH).format(l.getEndTime()) + "</td>");
                    } else {
                        out.println("<td>" + "auction" + "</td>");
                    }
                    //request.setAttribute("lot", l);
                    out.println("<td>" + "<a href=\"timer.jsp?id=" + l.getLotId() + "\">More Info</a>" + "</td>");
//                    RequestDispatcher dispatcher = request.getRequestDispatcher("timer.jsp");
//                    if (dispatcher != null) {
//                        dispatcher.forward(request, response);
//                    }

                    out.println("</tr>");
                } catch (Exception e) {
                    out.println("<td>" + "Error" + "</td>");
                }
//                if (count % 2 == 1) {
//                    out.println("</tr>");
//                }
            }
            out.println("</table>");
            out.println("</body>");
            out.println("</html>");
        } finally {
            em.close();
            emf.close();

            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
